import requests
import json
import pymongo

# mongo连接
mongo_conn = pymongo.MongoClient()

pageIndex = 0
while True:
    pageIndex +=1
    # 模拟请求
    url = "http://zjj.zhuzhou.gov.cn/fcjadpater/select/WWW_YSXK_002"
    data = {'verifycode': '', 'cdno': '', 'lname': '', 'pageIndex': pageIndex, 'pageSize': '15'}
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    response = requests.post(url, data=data, headers=headers)
    result = response.content.decode()
    print(response.status_code)
    print(result)

    # 数据解析 json反序列化解析需要的数据字段
    result = json.loads(result)
    rows = result.get("rows")
    if not rows:
        break
    # 二次json反序列化
    for row in json.loads(rows):
        cdno = row.get("cdno")
        lname = row.get("lname")
        fpath = "http://zjj.zhuzhou.gov.cn" + row.get("fpath")
        # with open(cdno + ".jpg", "wb") as f:
        #     f.write(requests.get(fpath).content)
        print(pageIndex,cdno, lname, fpath)